import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

# 数据获取
def read_data(file_dir:str): # 读取绘图数据和参数数据
    df = pd.read_excel(file_dir,
                       sheet_name = 'Data',
                       index_col = 0)
    params = pd.read_excel(file_dir,
                           sheet_name = 'Parameters',
                           index_col = 0)
    params = params['Param_value'].to_dict()
    return df,params

def zh_font(params): #是否显示中文
    if params['zh_font']==1:
        plt.rcParams['font.sans-serif']=['SimHei']
        plt.rcParams['axes.unicode_minus']=False
    else:
        pass

# 如果存在cmap_color,则使用它，如果不存在，则使用序列离散颜色。
def color_selector(df,params):
    cmap_color = params['cmap_color']
    if type(cmap_color) == str:
        cmaps = mpl.colormaps.get_cmap(cmap_color)
        cmaps = cmaps(np.linspace(0,1,len(df)))
        colors = cmaps
    elif np.isnan(cmap_color):
        colors = mpl.color_sequences[params.get('sequences_color')]
    return colors
def color_selector_1(params,num):
    cmap_color = params['cmap_color']
    if type(cmap_color) == str:
        cmaps = mpl.colormaps.get_cmap(cmap_color)
        cmaps = cmaps(np.linspace(0,1,num))
        colors = cmaps
    elif np.isnan(cmap_color):
        colors = mpl.color_sequences[params.get('sequences_color')]
    return colors

# 获取参数中的元组
def get_tuple(value:str):
    a = value.split(sep=',')
    a = tuple([int(i) for i in a])
    return a

def title_and_labels(params):
    plt.title(params['title'],
                 size = params['title_fontsize'])
    plt.xlabel(params['x_label'] if \
                  (type(params['x_label']) == str) else None,
                  size = params['label_fontsize'])
    plt.ylabel(params['y_label'] if \
                  (type(params['y_label']) == str) else None,
                  size = params['label_fontsize'])
# 调整标题和坐标轴
def title_and_axis_setting(df,params):
    plt.title(params['title'],
                 size = params['title_fontsize'])
    plt.xlabel(params['x_label'] if \
                  (type(params['x_label']) == str) else None,
                  size = params['label_fontsize'])
    plt.ylabel(params['y_label'] if \
                  (type(params['y_label']) == str) else None,
                  size = params['label_fontsize'])
    plt.xticks(range(len(df)),
                  labels = df.index,
                  rotation = params['xlabel_rotation'],
                  size = params['xlabel_fontsize'],
                  ha = params['xlabel_ha'] if \
                  (type(params['xlabel_ha'])==str) else 'right')

if __name__=='__main__':
    df,params = read_data('../test_data/简单柱状图.xlsx')
    print(df,params)
